<form id="myform" class="layui-form layui-form-pane" action="" style="margin: 5px">
    <!--<div class="layui-form-item">-->
        <!--<label class="layui-form-label">版本号</label>-->
        <!--<div class="layui-input-block">-->
            <!--<select id="versionid" name="versionid" required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input"/>-->
        <!--</div>-->
    <!--</div>-->
    <div class="layui-form-item">
        <label class="layui-form-label">编号</label>
        <div class="layui-input-block" >
            <input id="id" lay-filter="test1" type="text" name="id" required lay-verify="required" placeholder="请输入唯一标识名称（英文）" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">IP</label>
        <div class="layui-input-block" >
            <input id="ip" lay-filter="test1" type="text" name="ip" required lay-verify="required" placeholder="请输入唯一标识名称（英文）" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">连接串</label>
        <div class="layui-input-block" >
            <input id="url" lay-filter="test1" type="text" name="url" required lay-verify="required" placeholder="请输入唯一标识名称（英文）" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">HOST</label>
        <div class="layui-input-block" >
            <input id="host" lay-filter="test1" type="text" name="host" required lay-verify="required" placeholder="请输入唯一标识名称（英文）" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">端口</label>
        <div class="layui-input-block" >
            <input id="port" lay-filter="test1" type="text" name="port" required lay-verify="required" placeholder="请输入唯一标识名称（英文）" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block" >
            <input id="username" lay-filter="test1" type="text" name="username" required lay-verify="required" placeholder="请输入唯一标识名称（英文）" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block" >
            <input id="password" lay-filter="test1" type="text" name="password" required lay-verify="required" placeholder="请输入唯一标识名称（英文）" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block" >
            <input id="description" lay-filter="test1" type="text" name="description" required lay-verify="required" placeholder="请输入唯一标识名称（英文）" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">公司名称</label>
        <div class="layui-input-block" >
            <input id="companyName" lay-filter="test1" type="text" name="companyName" required lay-verify="required" placeholder="请输入唯一标识名称（英文）" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">公司名称</label>
        <div class="layui-input-block" >
            <input id="companyId" lay-filter="test1" type="text" name="companyId" required lay-verify="required" placeholder="请输入唯一标识名称（英文）" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">作者</label>
        <div class="layui-input-block" >
            <input id="author" lay-filter="test1" type="text" name="author" required lay-verify="required" placeholder="请输入唯一标识名称（英文）" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="connect_test">连接测试</button>
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script>
    layui.use(['jquery','baajax','form'], function(){
        var form = layui.form;
        var ajax=layui.baajax;
        var $=layui.jquery;
        getInfo();
        //监听提交
        form.on('submit(formDemo)', function(data){
            layer.msg(JSON.stringify(data.field));

            ajax.put("/metadatadatabase",data.field,function(data){
                if(data.code=='200'){
                    //layer.msg('添加成功');
                    layer.confirm('添加成功,是否继续添加?', {icon: 3, title:'提示'}, function(index){
                        //form.render(null, 'test1');
                        // $("#myform").get(0).reset();
                        layer.close(index);
                    },function(index){
                        //table.refresh();
                        layer.closeAll();
                    });
                }
            },function (data) {
                layer.msg('失败');
            });
            // $.ajax({
            //     type: "POST",
            //     url: "/metadatadatabase",
            //     dataType: "json",
            //     data: data.field,
            //     cache: true,
            //     async: false,
            //     success: function (data) {
            //         if(data.code=='200'){
            //             //layer.msg('添加成功');
            //             layer.confirm('添加成功,是否继续添加?', {icon: 3, title:'提示'}, function(index){
            //                 //form.render(null, 'test1');
            //                 // $("#myform").get(0).reset();
            //                 layer.close(index);
            //             },function(index){
            //                 //table.refresh();
            //                 layer.closeAll();
            //             });
            //         }
            //         // var ret = data;
            //         // succFunc(ret, paras);
            //     },
            //     error: function () {
            //         layer.msg('失败');
            //         // errorFunc();
            //     }
            // })
            return false;
        });
        //监听连接测试提交
        form.on('submit(connect_test)', function(data){
            layer.msg(JSON.stringify(data.field));
            $.ajax({
                type: "POST",
                url: "/metadatadatabase/connectTest",
                dataType: "json",
                data: data.field,
                cache: true,
                async: false,
                success: function (data) {
                    if(data.code=='200'){
                        layer.msg('连接成功');
                    }else{
                        layer.msg('连接失败');
                    }
                    // var ret = data;
                    // succFunc(ret, paras);
                },
                error: function () {
                    layer.msg('请求失败');
                    // errorFunc();
                }
            })
            return false;
        });

        function getInfo(){
            ajax.get("/metadatadatabase/"+$("#layer_metadata_databaseId").val(),{},function (data) {
                //if(data.code=='200'){
                //layer.msg('添加成功');
                $("#id").val(data.id);
                $("#ip").val(data.ip);
                $("#url").val(data.url);
                $("#host").val(data.host);
                $("#port").val(data.port);
                $("#username").val(data.username);
                $("#password").val(data.password);
                $("#description").val(data.description);
                $("#companyName").val(data.companyName);
                $("#companyId").val(data.companyId);
                $("#author").val(data.author);
                // $("#answer").val(data.answer);
                // $("#operator").val(data.operator);
                if(data.userflag===0){
                    $("#switch").prop('checked','checked');
                }else{
                    $("#switch").removeAttr('checked');
                }
                form.render('checkbox');
                //}
            },function () {
                layer.msg('查询失败');
            });
        }
    });
</script>